package bean.managed;

import entity.PackingOrderEntity;
import bean.managed.util.JsfUtil;
import bean.session.PackingOrderEntityFacade;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;
import managedbean.AbstractBacking;
import type.PackingOrderStatus;

@ManagedBean (name="packingOrderEntityController")
@SessionScoped
public class PackingOrderEntityController extends AbstractBacking implements Serializable {


    private PackingOrderEntity current;
    @EJB private bean.session.PackingOrderEntityFacade ejbFacade;

    public PackingOrderEntityController() {
    }

    public PackingOrderEntity getSelected() {
        if (current == null) {
            current = new PackingOrderEntity();
        }
        return current;
    }

    private PackingOrderEntityFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PackingOrderEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PackingOrderEntityUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        performDestroy();
        return "List";
    }

    public String destroyAndView() {
        performDestroy();
            return "List";
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PackingOrderEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass=PackingOrderEntity.class)
    public static class PackingOrderEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            PackingOrderEntityController controller = (PackingOrderEntityController)facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "packingOrderEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof PackingOrderEntity) {
                PackingOrderEntity o = (PackingOrderEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+PackingOrderEntityController.class.getName());
            }
        }

    }

    public PackingOrderEntity getCurrent() {
        return current;
    }

    public void setCurrent(PackingOrderEntity current) {
        this.current = current;
    }
    
    public void markUnpacked(){
        current.setStatus(PackingOrderStatus.pending);
        current.modified(getLoginPerson());
        update();
    }
    
    public void markPacked(){
        current.setStatus(PackingOrderStatus.packed);
        current.modified(getLoginPerson());
        update();
    }
    
    public boolean isPacked(){
        return current.getStatus()==PackingOrderStatus.packed;
    }

}
